package com.example.hello.controller;

import com.example.hello.common.Result;
import com.example.hello.dto.IdentityVerificationDTO;
import com.example.hello.dto.PasswordResetDTO;
import com.example.hello.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Tag(name = "密码管理", description = "密码找回和重置相关接口")
@RestController
@RequestMapping("/api/password")
public class PasswordController {
    
    private static final Logger logger = LogManager.getLogger(PasswordController.class);
    
    @Autowired
    private UserService userService;
    
    @Operation(summary = "验证用户身份", description = "通过用户名、邮箱和手机号验证用户身份")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "验证成功"),
            @ApiResponse(responseCode = "400", description = "验证失败")
    })
    @PostMapping("/verify-identity")
    public Result<Map<String, Object>> verifyIdentity(@Valid @RequestBody IdentityVerificationDTO verificationDTO) {
        Map<String, Object> result = userService.verifyIdentity(verificationDTO);
        
        if (result != null) {
            return Result.success(result);
        } else {
            return Result.error(400, "身份验证失败，请检查用户名、邮箱和手机号是否正确");
        }
    }
    
    @Operation(summary = "重置密码", description = "通过验证令牌重置密码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "密码重置成功"),
            @ApiResponse(responseCode = "400", description = "密码重置失败")
    })
    @PostMapping("/reset-password")
    public Result<Void> resetPassword(@Valid @RequestBody PasswordResetDTO resetDTO) {
        // 验证两次密码是否一致
        if (!resetDTO.getNewPassword().equals(resetDTO.getConfirmPassword())) {
            return Result.error(400, "两次输入的密码不一致");
        }
        Result<Void> result = userService.resetPassword(resetDTO);
        return result;
    }
} 